/*
* ============================================================================
*  Name     : LockServer.cpp
*  Part of  : LockServer
*  Created  : 25.12.2005 by DANG Zhengfa
*  Description:
*     Exe source file
*  Version  :
*  Copyright: Zhengfa Company Limited
* ============================================================================
*/


//  Include Files  

#include "LockServer.h"
#include "Log.h"
#include "Utilities.h"
#include "KeyLocker.h"
#include "KeyWatcher.h"
#include "ScreenCapture.h"


#include "Shared.h"   // for KTextEXETitle, KLockServerParameters

#include <e32base.h>
#include <e32std.h>
#include <f32file.h>   // For RFs, to load configuration from file

// Global variables. this is an exe, so there can be some data outside of class
const TUint gVersion = 20051229;
TUint gTimeInterval = 35;
TUint gAutoStart = ETrue;
TUint gEnableNote = ETrue;


void DoRealJobL(const TDesC& aArgs)
    {
		//
		// load configuration file
		//
		TBool loaded = LoadConfigurationL(gVersion,gTimeInterval,gAutoStart,gEnableNote);
		if( !loaded )
		{
			PrintString16(_L("Load settings failed, use default"));
		}
		
		// if autostart is negative, check parameter from KeyLocker
		//   we send _LIT8("manual") as parameter to this exe, but aArgs is TDesC, conversion is quite complicated
		//   so we simply check the length of aArgs, if it is not 7,quit
		/** You can use the following code to see what happens here
		PrintString16(_L("Argument and its size is:"));
		TBuf<256> para;
		TBuf8<256> para8;
		const TUint16 *ptr = aArgs.Ptr();
		para8.Copy((TUint8*)ptr,aArgs.Size());
		para.Copy(para8);
		PrintString16(para);
		PrintNumber(aArgs.Length());
		PrintNumber(aArgs.Size());
		************************/

		// there is another way to do this, when you use CApaCommandLine to launch the app, 
		// TPtrC para(KLockServerParameters);     // 16b descriptor
		// TPtrC8 para8( (TUint8*)para.Ptr(),para.Size() );  // 8b ptr
		// cmd->SetTailEndL(para8); // pass parameter to exe
		PrintString16(aArgs);
		PrintNumber(aArgs.Length());
		if( !gAutoStart && aArgs.Find(KLockServerParameters)==KErrNotFound) 
			{
				PrintString16(_L("!AutoStart & !Args, Quit"));
				return;
			}

		// Create key locker (active object)
		CKeyLocker *locker = new (ELeave) CKeyLocker();
		CleanupStack::PushL(locker);
		
		// set parameters for key locker
		locker->ConstructL();
		locker->SetNote(gEnableNote);
		locker->SetTimeInterval(gTimeInterval);
		
		// Start key locker
		locker->StartL();

		// create capture
		CScreenCapture *capture = new (ELeave) CScreenCapture();
		CleanupStack::PushL(capture);
		capture->ConstructL();

		// Create key watcher (active object)
		CKeyWatcher *watcher = new (ELeave) CKeyWatcher();
		CleanupStack::PushL(watcher);
		
		// construct and start key watcher
		watcher->ConstructL();
		watcher->SetKeyLocker(locker);
		watcher->SetScreenCapture(capture);
		watcher->StartL();
	
		// start active scheduler
		CActiveScheduler::Start();

		CleanupStack::PopAndDestroy(3);  // locker, capture, watcher
    }

void MainL()
    {
	// Create active scheduler (to run active objects)
    CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
    CleanupStack::PushL(scheduler);
    CActiveScheduler::Install(scheduler);
	
	// to make sure only one instance of this exe is running
	TInt num = CheckInstances(KTextEXETitle);
	PrintString16(_L("Number of instances:"));
	PrintNumber(num);
	if(num <= 1)  
	{
		// Call main function with command line
		TBuf<256> cmdLine;
		RProcess().CommandLine(cmdLine);
		DoRealJobL(cmdLine);
	}

	PrintString16(_L("Quit"));

    // Delete active scheduler
	CActiveScheduler::Install(NULL);
    CleanupStack::PopAndDestroy(scheduler);
	}


//  Global Functions

GLDEF_C TInt E32Main()
    {
    // Create cleanup stack
    __UHEAP_MARK;
    CTrapCleanup* cleanup = CTrapCleanup::New();


	// this Macro will run ConsoleMainL() if _DEBUG_CONSOLE_ is defined 
	//   otherwise, it will run MainL() directly
	//   ConsoleMainL() can be found in Console.cpp
	RUNMAIN(); // Macro
	
	// error is defined in TRAPD in  RUNMAIN
	__ASSERT_ALWAYS(!error,User::Panic(KTextEXETitle,error));

	delete cleanup;
    __UHEAP_MARKEND;

	return KErrNone;
    }

// End of file
